const { requestResponse, requestStream } = RSocket;

/**
 * 登录认证
 * @param {{username: string, password: string}} data 认证参数
 * @returns {{ expires: number, token: string }} 认证结果
 */
export function auth(data) {
  return requestResponse({
    route: 'auth',
    data: { ...data, scope: SCOPE },
  });
}
export function authSms(data) {
  return requestResponse({
    route: 'auth.sms',
    data: { ...data, scope: SCOPE },
  });
}
// 获取图形验证码
export function authCode() {
  return requestResponse({
    route: 'auth.captcha',
  });
}

/**
 * 获取当前账户信息
 * @returns object
 */
export async function me() {
  return RSocket.rr({
    route: 'users.me',
  });
}

export function getStatistics() {
  return requestResponse({
    route: 'subscribe.statistics',
  });
}

export function getEndpoints() {
  return requestStream({
    route: `endpoints.page.${SCOPE}`,
  });
}

/**
 * 修改密码
 * @param data
 */
export function update(data) {
  return requestResponse({
    route: 'users.changepassword',
    data,
  });
}

export function refreshToken(oldToken) {
  // return Api.post(`/token/refresh?token=${oldToken}`);
  return requestResponse({
    route: 'token.refresh',
    args: {
      token: oldToken,
    },
  });
}

/**
 * 获取手机验证码
 * @param data
 */
export function getPhoneCode(data) {
  return RSocket.rr({
    route: 'auth.sms.sendcode',
    data: {
      ...data,
    },
  });
}

/**
 * 获取用户权限
 * @param data
 */
export function getRolesBelong(username) {
  return RSocket.rs({
    route: `roles.${username}.belong`,
  });
}

export function getCode() {
  return RSocket.rr({
    route: 'auth.code',
  });
}

export function updateUser(user) {
  return RSocket.rr({ route: 'users.update', data: user });
}
